package cn.sunlight.mm.controller;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.framework.annotation.HmRequestMapping;
import cn.sunlight.framework.annotation.HmSetter;
import cn.sunlight.mm.base.BaseController;
import cn.sunlight.mm.common.GlobalConst;
import cn.sunlight.mm.entity.Result;
import cn.sunlight.mm.pojo.ReviewLog;
import cn.sunlight.mm.pojo.User;
import cn.sunlight.mm.security.HmAuthority;
import cn.sunlight.mm.service.ReviewLogService;
import cn.sunlight.mm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author sunlight
 * @description: 审核日志控制器
 * @date 2021/5/25
 * @Version v1.0
 */
@Slf4j
@HmComponent
public class ReviewLogController extends BaseController {

    @HmSetter("reviewLogService")
    private ReviewLogService reviewLogService;

    @HmAuthority("QUESTION_REVIEW_UPDATE")
    @HmRequestMapping("/review/add")
    public void addReviewLog(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            ReviewLog reviewLog = parseJSON2Object(request, ReviewLog.class);
            log.debug("reviewLog:{}",reviewLog);
            //获取用户
            User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);
            //初始化数据
            reviewLog.setUserId(user!=null?user.getId():1);
            reviewLog.setCreateDate(DateUtils.parseDate2String(new Date()));

            log.debug("调用service：{}",reviewLog);

            reviewLogService.addReviewLog(reviewLog);

            printResult(response,new Result(true,""));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,""+e.getMessage()));
        }
    }

}
